Skip to content

[PHP] Prepare cautious URL mappings once - #588

Merged
adamziel merged 1 commit into
trunkfrom
codex/prepare-cautious-url-mappings
Aug 14, 2026
Merged

[PHP] Prepare cautious URL mappings once#588
adamziel merged 1 commit into
trunkfrom
codex/prepare-cautious-url-mappings

Conversation

@adamziel

@adamziel adamziel commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator

Prepares cautious URL mappings once, then shares them with every text processor created by one StructuredDataUrlRewriter.

Before this PR, each processor parsed the same source and target URLs, sorted them, and built the same regular expressions again. A database value may contain many text leaves, so that setup work was repeated for every leaf.

CautiousURLBaseRewriteMapping now owns that setup. A caller which processes several values can reuse it directly:

$url_mapping = new CautiousURLBaseRewriteMapping([
    'https://source.example/media' =>
        'https://destination.example/assets',
]);

foreach ($text_values as $text) {
    $processor = new CautiousURLBaseProcessorInTextWithMixedUnknownEscapeRules(
        $text,
        $url_mapping
    );

    while ($processor->next_url()) {
        $processor->replace_url_base();
    }

    $rewritten_values[] = $processor->get_updated_text();
}

StructuredDataUrlRewriter creates this object in its constructor and passes it to its plain-text and block-markup processors. The mapping is prepared once even when serialized PHP, JSON, or block markup exposes many separate text values.

In the local benchmark, constructing a processor with one mapping fell from 1.75 µs to 0.09 µs. With 64 mappings it fell from 106 µs to 0.09 µs. Rewriting one short matching value fell from 3.23 µs to 1.37 µs.

Testing

The URL-rewriting suite passes with 363 tests and 2,502 assertions. The focused cautious and structured suites pass with 180 tests and 219 assertions. PHPStan and the coding-standard checks pass for the changed classes.

@github-actions

github-actions Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Pull pipeline performance — large-directory

Site: large-directory · 2,000+ plus targeted file-transfer scenarios files · 10,000 posts · 25,000 postmeta · PHP 8.5.9

Stage PR trunk Δ Status Details
playground-sqlite-db-pull 9.86 s 9.67 s ⚪ +187 ms (+1.9%) condition=db-pull in PHP.wasm
runtime=php.wasm 8.3
wp_mysql_parser=enabled
mode=lexer
native_lexer=verified
native_token_stream=WP_MySQL_Native_Token_Stream
native_token_count=18
native_parser=selected
trunk: condition=db-pull in PHP.wasm
runtime=php.wasm 8.3
wp_mysql_parser=enabled
mode=lexer
native_lexer=verified
native_token_stream=WP_MySQL_Native_Token_Stream
native_token_count=18
native_parser=selected
playground-sqlite-db-apply 3.82 s 3.70 s ⚪ +114 ms (+3.1%) condition=db-apply to SQLite in PHP.wasm
runtime=php.wasm 8.3
wp_mysql_parser=enabled
mode=parser
native_lexer=verified
native_token_stream=WP_MySQL_Native_Token_Stream
native_token_count=18
native_parser=verified
native_ast=WP_MySQL_Native_Parser_Node
sqlite_driver_parser=verified
trunk: condition=db-apply to SQLite in PHP.wasm
runtime=php.wasm 8.3
wp_mysql_parser=enabled
mode=parser
native_lexer=verified
native_token_stream=WP_MySQL_Native_Token_Stream
native_token_count=18
native_parser=verified
native_ast=WP_MySQL_Native_Parser_Node
sqlite_driver_parser=verified
Total 13.68 s 13.38 s ⚪ +300 ms (+2.2%)

Numbers carry runner noise; treat single-run deltas as directional, not authoritative.

📈 Trunk performance history — commit-by-commit timeline.

@adamziel
adamziel force-pushed the codex/prepare-cautious-url-mappings branch from c5f11be to 6fd889c Compare August 14, 2026 00:25
@adamziel
adamziel requested a balanced review from Copilot August 14, 2026 19:52

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Prepares cautious URL mappings once per structured-data rewriter, reducing repeated validation, sorting, and pattern construction.

Changes:

  • Introduces a reusable prepared mapping object.
  • Shares prepared mappings across plain-text and block-markup processors.
  • Adds reuse coverage and updates processor construction.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/UrlRewriting/CautiousURLBaseProcessorInTextWithMixedUnknownEscapeRulesTest.php Tests prepared mapping reuse.
packages/reprint-client/src/lib/url-rewrite/load.php Loads the new mapping class.
packages/reprint-client/src/lib/url-rewrite/class-structured-data-url-rewriter.php Creates and shares one prepared mapping.
packages/reprint-client/src/lib/url-rewrite/class-cautious-url-base-rewrite-mapping.php Implements mapping preparation.
packages/reprint-client/src/lib/url-rewrite/class-cautious-url-base-processor-in-text-with-mixed-unknown-escape-rules.php Consumes prepared mappings.
packages/reprint-client/src/lib/url-rewrite/class-cautious-text-block-markup-url-processor.php Reuses prepared mappings for markup tokens.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@adamziel
adamziel merged commit f55ad8a into trunk Aug 14, 2026
22 checks passed
@adamziel
adamziel deleted the codex/prepare-cautious-url-mappings branch August 14, 2026 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants